var util = require('../../utils/util.js');
var qcloud = require('../../vendor/wafer2-client-sdk/index')
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrls: [
      {
        link: '/pages/index/index',
        url: 'https://tiaonali.com/zhiwei31.jpg'
      }, {
        link: '/pages/news/index',
        url: 'https://tiaonali.com/zhiwei3.jpg'
      }
    ],
    getHunterList:[],
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    winWidth: 0,
    winHeight: 0,
    currentTab: 0,
    userInfo: {},
    pageNum: 1,
    isFirstLoad: true,
    noMore: false,
    curr_page_no:1,
    totalPages:0,
    banner:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
    util.post(app.api.getAdcarouse, { type:2 }, function (res) {
      //获取首页轮播图
      that.setData({
        banner: res.data
      });
      console.log(that.data.banner);
    });
    this.resetData(true);
  },

resetData:function(isRefresh){
  var that=this;
  var pageNo=1;
  if (!isRefresh){
    //加载更多
    pageNo = that.data.curr_page_no+1;
  }
  util.post(app.api.getHunterList, { page: pageNo, size: 10 }, function (res) {
    if (isRefresh){
      //执行刷新 当前页号重置为1
      that.setData({
        curr_page_no: 1,
        noMore:false
      });
    }else{
      //加载更多
      that.setData({
        curr_page_no: pageNo
      });
    }
    if (res != null && res.data != null && res.data.data.length > 0) {
      that.setData({
        getHunterList: res.data.data,
        totalPages: res.data.pageNum
      });
    }else{
      wx.showToast({
        title: '没有数据',
        icon: 'loading',
        duration: 1200
      })
    }

    console.log(that.data.noMore);
  });
},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    console.log("onShow");
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    console.log("onHide");
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that=this;
    // 显示导航栏loading  
    wx.showNavigationBarLoading();
    // 调用接口加载数据  
    this.resetData(true);
    // 隐藏导航栏loading  
    wx.hideNavigationBarLoading();
    // 当处理完数据刷新后，wx.stopPullDownRefresh可以停止当前页面的下拉刷新  
    wx.stopPullDownRefresh();  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    if (that.data.curr_page_no>=that.data.totalPages){
      console.log("onReachBottom noMore ");
      //已经是最后一页
      that.setData({
        noMore:true,
      });
    }else{
      this.resetData(false);
    }
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //猎头详细信息
  hunterTap: function (event) {
    var id = event.currentTarget.dataset.id; // 当前id
    console.log("猎头详情id获取=" + id)
    // 切换页面
    wx.navigateTo({
      url: '../hunter-detail/hunter-detail?hunterid=' + id
    });
  }
})